/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.web.cars;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.annotation.ExcelField;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.base.entity.cars.Tcar;
import com.jeesite.modules.base.entity.cars.TcarConfiguration;
import com.jeesite.modules.base.service.cars.TcarService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 车辆档案Controller
 * @author shengbin
 * @version 2021-03-01
 */
@Controller
@RequestMapping(value = "${adminPath}/base/cars/tcar")
public class TcarController extends BaseController {

	@Autowired
	private TcarService tcarService;
    @Autowired
    private UserService userService;

	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Tcar get(String carId, boolean isNewRecord) {
		return tcarService.get(carId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("base:cars:tcar:view")
	@RequestMapping(value = {"list", ""})
	public String list(Tcar tcar, Model model) {
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("tcar", tcar);
		return "modules/base/cars/tcarList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("base:cars:tcar:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Tcar> listData(Tcar tcar, HttpServletRequest request, HttpServletResponse response) {
		tcar.setPage(new Page<>(request, response));
		Page<Tcar> page = tcarService.findPage(tcar);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("base:cars:tcar:view")
	@RequestMapping(value = "form")
	public String form(Tcar tcar, Model model) {
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("tcar", tcar);
		return "modules/base/cars/tcarForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("base:cars:tcar:view")
	@RequestMapping(value = "detail")
	public String detail(Tcar tcar, Model model) {
        model.addAttribute("listUser", userService.findList(new User()));
		model.addAttribute("tcar", tcar);
		return "modules/base/cars/tcarDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("base:cars:tcar:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Tcar tcar) {
		tcarService.save(tcar);
		return renderResult(Global.TRUE, text("保存车辆档案成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("base:cars:tcar:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Tcar tcar) {
		tcar.setStatus(Tcar.STATUS_DISABLE);
		tcarService.updateStatus(tcar);
		return renderResult(Global.TRUE, text("停用车辆档案成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("base:cars:tcar:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Tcar tcar) {
		tcar.setStatus(Tcar.STATUS_NORMAL);
		tcarService.updateStatus(tcar);
		return renderResult(Global.TRUE, text("启用车辆档案成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("base:cars:tcar:delete")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Tcar tcar) {
		tcarService.delete(tcar);
		return renderResult(Global.TRUE, text("删除车辆档案成功！"));
	}

	/**
	 * 导出用户数据
	 */
	@RequiresPermissions("base:cars:tcar:import")
	@RequestMapping(value = "exportData")
	public void exportData(Tcar tcar, Boolean isAll, String ctrlPermi, HttpServletResponse response) {
		/*if (!(isAll != null && isAll) || Global.isStrictMode()) {
			tcarService.addDataScopeFilter(tcar, ctrlPermi);
		}*/
		List<Tcar> list = tcarService.findList(tcar);
		String fileName = "车辆信息数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try (ExcelExport ee = new ExcelExport("车辆信息数据", Tcar.class)) {
			ee.setDataList(list).write(response, fileName);
		}
	}

	/**
	 * 下载导入用户数据模板
	 */
	@RequiresPermissions("base:cars:tcar:import")
	@RequestMapping(value = "importTemplate")
	public void importTemplate(HttpServletResponse response) {
		TcarConfiguration tcarConfiguration = new TcarConfiguration();
		List<TcarConfiguration> list = ListUtils.newArrayList(tcarConfiguration);
		String fileName = "车辆配置信息模板.xlsx";
		try (ExcelExport ee = new ExcelExport("车辆配置信息模板", TcarConfiguration.class, ExcelField.Type.IMPORT)) {
			ee.setDataList(list).write(response, fileName);
		}
	}

	/**
	 * 导入用户数据
	 */
	@ResponseBody
	@RequiresPermissions("base:cars:tcar:import")
	@PostMapping(value = "importData")
	public String importData(MultipartFile file) {
		try {
			String message = tcarService.importData(file);
			return renderResult(Global.TRUE, "posfull:" + message);
		} catch (Exception ex) {
			return renderResult(Global.FALSE, "posfull:" + ex.getMessage());
		}
	}




	
}